<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <todo-list>
            <!-- 默认插槽（因为只有一个 slot，可省略 template 标签）
                如果要加，将 <template v-slot> 加到 todo-item 外面
             -->
			<todo-item @delete="handleDelete" v-for="item in list" :title="item.title" :del="item.del">
				<!-- 传入具名插槽 v-slot:插槽名称 -->
				<!-- vue 2.6 写法 -->
				<template v-slot:pre-icon>
					<span>前置</span>
				</template>
				<template v-slot:suf-icon>
					<span>后置</span>
				</template>
				<!-- vue 2.5 写法
				<span slot="pre-icon">前置</span>
				<span slot="suf-icon">后置</span>
				-->
			</todo-item>
		</todo-list>
    </div>
    <script src="./script/vue.js"></script>
    <script>
        Vue.component('todo-item', {
            props: {
                title: String,
                del: {
                    type: Boolean,
                    default: false,
                },
            },
            template: `
            <li>
				<slot name="pre-icon"></slot>
                <span v-if="!del">{{title}}</span>
                <span v-else style="text-decoration: line-through">{{title}}</span>
                <button @click="handleClick" v-show="!del">删除</button>
				<slot name="suf-icon"></slot>
            </li>
          `,
            data: function() {
                return {}
            },
            methods: {
				handleClick : function() {
					console.log('click del!');
					<!-- 触发自定义事件 -->
                    this.$emit('delete', this.title)
				}
            },
        })
        Vue.component('todo-list', {
            template: `
            <ul>
              <slot></slot>
            </ul>
          `,
            data: function() {
                return {
                }
            },
        })
        var vm = new Vue({
            el: '#app',
            data: {
				list: [{
					title: '课程1',
					del: false
				}, {
					title: '课程2',
					del: true
				}],
			},
			methods: {
				handleDelete : function(title) {
					console.log('title = ' + title);
				}
			},
        })
    </script>
</body>

</html>